package com.offcn;

import java.util.Stack;

/*
编写一个类，用两个栈实现队列，支持队列的基本操作（add、poll、peek）
 */
public class Queen_FirstWay {
    public static void main(String[] args) {
        MyQueen queen = new MyQueen();
        /*queen.add(123);
        queen.add(153);
        queen.add(993);
        queen.add(188);
        queen.add(1283);

        System.out.println(queen);

        System.out.println(queen.poll());
        System.out.println(queen.poll());
        System.out.println(queen.poll());
        System.out.println(queen.poll());
        System.out.println(queen.poll());
        System.out.println(queen.poll());*/
        queen.add(153);
        queen.add(993);
        queen.add(188);

        queen.poll();

        queen.add(153);
        queen.add(993);
        queen.add(188);

        queen.poll();

    }
}

class MyQueen {
    //数据压入栈
    private Stack<Integer> stackAdd;
    //数据弹出栈
    private Stack<Integer> stackPoll;

    public MyQueen() {
        this.stackAdd = new Stack<>();
        this.stackPoll = new Stack<>();
    }
    //将数据一次压入栈中
    public void addToPoll() {
        if(stackPoll.empty()) {
            while (!stackAdd.empty()) {
                stackPoll.push(stackAdd.pop());
            }
        }
    }

    public void add(int num) {
        stackAdd.push(num);
        addToPoll();
    }

    public int poll() {
       if(stackAdd.empty() && stackPoll.empty()) {
           throw new RuntimeException("The quenu is empty ");
       }
       addToPoll();
       return stackPoll.pop();
    }

    public int peek() {
        if(stackPoll.empty() && stackAdd.isEmpty()) {
            throw new RuntimeException("Quenu is empty");
        }

        addToPoll();
        return stackPoll.peek();
    }

    @Override
    public String toString() {
        return "MyQueen{" +
                 stackAdd +
                '}';
    }
}
